package com.iwown.sample4GApi.calculation;

import com.google.protobuf.InvalidProtocolBufferException;
import com.iwown.sample4GApi.protobuf.HisDataOuterClass;
import com.iwown.sample4GApi.protobuf.HisEcgData;
import com.iwown.sample4GApi.utils.MyDateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class EcgPreprocessor {
    private static final Logger log = LoggerFactory.getLogger(EcgPreprocessor.class);

    public void DataPrepare(byte[] payload) {
        HisDataOuterClass.HisNotification hisNotify = null;
        try {
            hisNotify = HisDataOuterClass.HisNotification.parseFrom(payload);
        } catch (InvalidProtocolBufferException ex) {
            ex.printStackTrace();
            return;
        }
        if(hisNotify.hasHisData()){
            HisDataOuterClass.HisData hisData = hisNotify.getHisData();
            if(hisData.hasEcg()){
                HisEcgData.HisDataECG ecgData = hisData.getEcg();
                /*
                	save rawDataList with data_time, later combine all ecg data
                	with the same data_time, same data_time means it's the same
                	ecg measurement
                 */
                String data_time = MyDateTimeUtils.convert2DateTimeString(ecgData.getTimeStamp().getDateTime().getSeconds());
                List<Integer> rawDataList = ecgData.getRawDataList();
                log.info(String.format("%s ecg count:%d",data_time,rawDataList.size()));
            }
        }
    }
}
